package com.yx.cloud.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.airbnb.lottie.LottieAnimationView;
import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.bean.VoiceInfoBean;
import com.yx.cloud.utils.RomUtils;

import java.util.List;

public class VoiceListAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private final static String TAG = "VoideListAdapter";
    private List<VoiceInfoBean> mListData;
    private Context mContext;
    private View view;

    private OnItemClickListener mOnItemClickListener = null;

    public VoiceListAdapter(Context context, List<VoiceInfoBean> list) {
        this.mContext = context;
        this.mListData = list;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        view = LayoutInflater.from(mContext).inflate(R.layout.voice_list_item, parent, false);
        ViewHolder vh = new ViewHolder(view);
        LinearLayout llVoice = view.findViewById(R.id.ll_voiceStandard);
        llVoice.setOnClickListener(this);
        return vh;
    }

    public void updateVoiceList(List<VoiceInfoBean> voiceList) {
        mListData = voiceList;
        notifyDataSetChanged();
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ViewHolder) {
            String language = "中文";
            String language1 = "中文";
            int resId = 0;
            VoiceInfoBean result = mListData.get(position);
            Log.d(TAG, " onBindViewHolder----> " + result.getName());
            if (result.getShowName().equals("Chinese")) {
                //中文
                language = mContext.getString(R.string.MO_Voice_Chs);
                language1 = "中文";
                resId = R.mipmap.chinese_icon;
            } else if (result.getShowName().equals("葡萄牙语")) {
                //葡萄牙语
                language = "葡萄牙语";
                language1 = "Português";
                resId = R.mipmap.portuguese_icon;
            } else if (result.getShowName().equals("English")) {
                //英语
                language = mContext.getString(R.string.MO_Voice_En_Tip);
                language1 = "English";
                resId = R.mipmap.english_icon;
            } else if (result.getShowName().equals("德语")) {
                //德语
                language = "德语";
                language1 = "Deutsch";
                resId = R.mipmap.german_icon;
            } else if (result.getShowName().equals("西班牙语")) {
                //西班牙语
                language = "西班牙语";
                language1 = "Español";
                resId = R.mipmap.spanish_icon;
            } else if (result.getShowName().equals("法语")) {
                //法语
                language = "法语";
                language1 = "Français";
                resId = R.mipmap.french_icon;
            } else if (result.getShowName().equals("意大利语")) {
                //意大利语
                language = "意大利语";
                language1 = "Italiano";
                resId = R.mipmap.italian_icon;
            } else if (result.getShowName().equals("俄语")) {
                //俄语
                language = "俄语";
                language1 = "Pусский";
                resId = R.mipmap.russian_icon;
            } else if (result.getShowName().equals("日语")) {
                //日语
                language = "日语";
                language1 = "日本語";
                resId = R.mipmap.japanese_icon;
            } else if (result.getShowName().equals("希伯来语")) {
                //希伯来语
                //Hebrew
                language = "希伯来语";
                language1 = "היברית";
                resId = R.mipmap.hebrew_icon;
            }
            ((ViewHolder) holder).mIvPic.setImageResource(resId);
            ((ViewHolder) holder).mVoideName01.setText(language1);
            ((ViewHolder) holder).mVoideName02.setText(language);
            Log.d(TAG, result.getStauts() + " onBindViewHolder-222---> " + result.isSelected());
            if (result.isSelected()) {
                if (result.getStauts() == 1 || result.getStauts() == 2) {
                    ((ViewHolder) holder).mlanimatorView.setVisibility(View.VISIBLE);
                    ((ViewHolder) holder).mllVoiceStandard.setBackgroundResource(R.drawable.shape_text_background1);
                    ((ViewHolder) holder).mTvVoiceUse.setText(mContext.getResources().getString(R.string.U_Loading));
                    ((ViewHolder) holder).mTvVoiceUse.setTextColor(mContext.getResources().getColor(R.color.button_color2));
                } else if (result.getStauts() == 3 || result.getStauts() == -1) {
                    ((ViewHolder) holder).mlanimatorView.setVisibility(View.GONE);
                    ((ViewHolder) holder).mllVoiceStandard.setBackgroundResource(R.drawable.btn_background_selector);
                    ((ViewHolder) holder).mTvVoiceUse.setText(mContext.getResources().getString(R.string.MO_Using_Btn));
                    ((ViewHolder) holder).mTvVoiceUse.setTextColor(mContext.getResources().getColor(R.color.background_color));
                } else if (result.getStauts() == 0) {
                    //处理下载失败的情况（暂不处理）
                } else {
                    ((ViewHolder) holder).mlanimatorView.setVisibility(View.GONE);
                    ((ViewHolder) holder).mllVoiceStandard.setBackgroundResource(R.drawable.shape_text_background1);
                    ((ViewHolder) holder).mTvVoiceUse.setText(mContext.getResources().getString(R.string.MO_Use_Btn));
                    ((ViewHolder) holder).mTvVoiceUse.setTextColor(mContext.getResources().getColor(R.color.button_color2));
                }
            } else {
                ((ViewHolder) holder).mlanimatorView.setVisibility(View.GONE);
                ((ViewHolder) holder).mllVoiceStandard.setBackgroundResource(R.drawable.shape_text_background1);
                ((ViewHolder) holder).mTvVoiceUse.setText(mContext.getResources().getString(R.string.MO_Use_Btn));
                ((ViewHolder) holder).mTvVoiceUse.setTextColor(mContext.getResources().getColor(R.color.button_color2));
            }

        }

        ((ViewHolder) holder).mllVoiceStandard.setTag(position);
    }

    @Override
    public int getItemCount() {
        return mListData.size();
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        if (mOnItemClickListener != null) {
            mOnItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    /**
     * Item点击事件方法
     *
     * @param listener
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        TextView mVoideName01;
        TextView mVoideName02;
        LinearLayout mllVoiceStandard;
        LottieAnimationView mlanimatorView;
        TextView mTvVoiceUse;
        ImageView mIvPic;

        public ViewHolder(View itemView) {
            super(itemView);
            mVoideName01 = itemView.findViewById(R.id.viodeName01);
            mVoideName02 = itemView.findViewById(R.id.viodeName02);
            mllVoiceStandard = itemView.findViewById(R.id.ll_voiceStandard);
            mTvVoiceUse = itemView.findViewById(R.id.tv_voice_use);
            mlanimatorView = itemView.findViewById(R.id.voice_loading_animator);
            mIvPic = itemView.findViewById(R.id.iv_pic);
            mlanimatorView.setVisibility(View.GONE);
        }
    }

}
